//
//  CategoryHeaderView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/30.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CategoryHeaderView.h"

#import "LXOptionButton.h"

#import "CategoryPopView.h"

#import "SmartSortPopView.h"

#import "FilterPopView.h"

@interface CategoryHeaderView()<SmartSortPopViewDelegate,FilterPopViewDelegate,CategoryPopViewDelegate>{
    CGFloat _margin;
}

@property(nonatomic,strong)UIWindow * window;

@property(nonatomic,strong)UIButton * contentView;

@property(nonatomic,strong)LXOptionButton * category;

@property(nonatomic,strong)LXOptionButton * sort;

@property(nonatomic,strong)LXOptionButton * filter;

@property(nonatomic,strong)CategoryPopView * categorypopView;

@property(nonatomic,strong)SmartSortPopView * smartSortPopView;

@property(nonatomic,strong)FilterPopView * filterPopView;

@property(nonatomic,strong)UIImageView * arrowImageView;

@property(nonatomic,strong)id isPopView;

@property(nonatomic,strong)id willPopView;


@end
@implementation CategoryHeaderView



-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        _margin = 64;
        self.contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        [self.contentView addTarget:self action:@selector(hiddenAll) forControlEvents:UIControlEventTouchUpInside];
        self.contentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        
        self.category = [[LXOptionButton alloc] init];
        self.category.titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.category];
        [self.category addTarget:self action:@selector(popCategorypopView) forControlEvents:UIControlEventTouchUpInside];

        self.sort = [[LXOptionButton alloc] init];
        self.sort.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.sort addTarget:self action:@selector(popSmartSortPopView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.sort];
        
        self.filter = [[LXOptionButton alloc] init];
        self.filter.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.filter addTarget:self action:@selector(popFilterPopView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.filter];
        
        UIView * underLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-1, self.width, 1)];
        underLine.backgroundColor = HexColor(@"#EEEEEE");
        [self addSubview:underLine];
//        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popViewHaveHidden:) name:@"CategoryPopViewHidden" object:nil];
        
        self.sortType = 0;
        self.priceRane = 0;
        self.discounts = nil;
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat width = self.width/3;
    CGFloat height = self.height;
    
    [self.category setTitle:@"分类" forState:UIControlStateNormal];
    [self.category setImage:[UIImage imageNamed:@"arrow_2"] forState:UIControlStateNormal];
    self.category.size = CGSizeMake(width, height);
    self.category.style = LXOptionButtonStyleR_L;
    self.category.x = 0;
    
    [self.sort setTitle:@"智能排序" forState:UIControlStateNormal];
    [self.sort setImage:[UIImage imageNamed:@"arrow_2"] forState:UIControlStateNormal];
    self.sort.size = CGSizeMake(width, height);
    self.sort.style = LXOptionButtonStyleR_L;
    self.sort.x = self.category.right;
    
    [self.filter setTitle:@"筛选" forState:UIControlStateNormal];
    [self.filter setImage:[UIImage imageNamed:@"arrow_2"] forState:UIControlStateNormal];
    self.filter.size = CGSizeMake(width, height);
    self.filter.style = LXOptionButtonStyleR_L;
    self.filter.x = self.sort.right;
    
    [self addSubview:self.arrowImageView];
}


#pragma mark - smartSortPopView delegate
-(void)SmartSortPopView:(SmartSortPopView *)smartSortPopView didSelectedsortType:(NSInteger)type{
    self.sortType = type;
    [self hiddenAll];
}


#pragma mark - FilterPopView Delegate
-(void)FilterPopView:(FilterPopView *)filterPopView didSelectedWithPriceRangeType:(NSInteger)type discounts:(NSArray *)discounts{
    self.priceRane = type;
    self.discounts = discounts;
    [self hiddenAll];
}
#pragma mark - CategoryPopView Delegate
-(void)CategoryPopView:(CategoryPopView *)categoryPopView didSelectedWithCat_id:(NSString *)cat_id{
    self.cat_id = cat_id;
    [self hiddenAll];
}

-(void)setCategoryArray:(NSArray *)categoryArray{
    _categoryArray = categoryArray;
    self.categorypopView.categoryArray = _categoryArray;
//    self.cat_id = [_categoryArray.firstObject cat_id];
}


-(CategoryPopView *)categorypopView{
    if(_categorypopView == nil){
        _categorypopView = [[CategoryPopView alloc] initWithFrame:CGRectMake(0, _margin, ScreenWidth, 400)];
        _categorypopView.cat_id = self.cat_id;
        _categorypopView.delegate = self;
    }
    return _categorypopView;
}


-(SmartSortPopView *)smartSortPopView{
    if(_smartSortPopView == nil){
        _smartSortPopView = [[SmartSortPopView alloc] initWithFrame:CGRectMake(0, _margin, ScreenWidth, ScreenHeight - 64 - self.height)];
        _smartSortPopView.delegate = self;
    }
    return _smartSortPopView;
}


-(FilterPopView *)filterPopView{
    if(_filterPopView == nil){
        _filterPopView = [[FilterPopView alloc] initWithFrame:CGRectMake(0, _margin, ScreenWidth, ScreenHeight - 64 - self.height)];
        _filterPopView.delegate = self;
    }
    return _filterPopView;
}

-(UIImageView *)arrowImageView{
    if(_arrowImageView == nil){
        _arrowImageView = [[UIImageView alloc] init];
        _arrowImageView.image = [UIImage imageNamed:@"arrow_3"];
        _arrowImageView.size = _arrowImageView.image.size;
        _arrowImageView.hidden = YES;
        _arrowImageView.y = self.height - self.arrowImageView.height+2;
    }
    return _arrowImageView;
}

-(void)popCategorypopView{
    [self hiddenIsPopView];
    self.isPopView = self.categorypopView;
    [self.window addSubview:self.contentView];
    [self.contentView addSubview:self.categorypopView];
}

-(void)popSmartSortPopView{
    [self hiddenIsPopView];
    self.isPopView = self.smartSortPopView;
    [self.window addSubview:self.contentView];
    [self.contentView addSubview:self.smartSortPopView];
}

-(void)popFilterPopView{
    [self hiddenIsPopView];
    self.isPopView = self.filterPopView;
    [self.window addSubview:self.contentView];
    [self.contentView addSubview:self.filterPopView];
}

-(void)hiddenIsPopView{
    if(self.isPopView){
        [self.isPopView removeFromSuperview];
    }
}

-(void)hiddenAll{
    for (UIView *v  in self.contentView.subviews) {
        [v removeFromSuperview];
    }
    [self.contentView removeFromSuperview];
    if(self.delegate&&[self.delegate respondsToSelector:@selector(CategoryHeaderView:hiddenWithCat_id:sortType:priceRange:discounts:)]){
        [self.delegate CategoryHeaderView:self hiddenWithCat_id:self.cat_id sortType:self.sortType priceRange:self.priceRane discounts:self.discounts];
    }
}

-(void)setIsPopView:(id)isPopView{
    _isPopView = isPopView;
    if(!_isPopView){
        if([self.delegate respondsToSelector:@selector(CategoryHeaderView:hiddenWithCat_id:sortType:priceRange:discounts:)]){
            [self.delegate CategoryHeaderView:self hiddenWithCat_id:self.cat_id sortType:self.sortType priceRange:self.priceRane discounts:self.discounts];
        }
        self.arrowImageView.hidden = YES;
    }
}
-(void)hidden{
    [self hiddenAll];
}
-(void)popViewHaveHidden:(NSNotification*)noti{
    self.arrowImageView.hidden = YES;
    [noti.userInfo[@"popView"] removeFromSuperview];
    if(self.isPopView == self.willPopView){
        if([self.delegate respondsToSelector:@selector(CategoryHeaderView:hiddenWithCat_id:sortType:priceRange:discounts:)]){
            [self.delegate CategoryHeaderView:self hiddenWithCat_id:self.cat_id sortType:self.sortType priceRange:self.priceRane discounts:self.discounts];
        }
    }
    
}

-(UIWindow*)window{
    if(_window == nil){
        _window = [[UIApplication sharedApplication].delegate window];
    }
    return _window;
}
@end
